/*
 * Copyright 2010, 2011 by the University of Richmond, Richmond, VA 23173
 * Written by Eric F. Palmer eric@ericfpalmer.com || epalmer@richmond.edu
 * And other contributors
 * http://www.richmond.edu/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Project files reside at http://code.google.com/p/files-2-exist-db
 */
/*
 * epalmer
 * 2011-07-07 extend to allow folder recursing when it is desired
 */


package edu.richmond.is.webservices.util;

import edu.richmond.is.webservices.files2exist.Folder2ExistSettings;

import java.io.*;
import java.util.*;

public class FilesInFolderList {

  private File fileFolder;
  private ArrayList<File> fileFolderList = new ArrayList<File>();
  private ArrayList<File> folderList     = new ArrayList<File>();
  private ArrayList<File> fileArrayList  = new ArrayList<File>();

  private boolean recurseFolderFlag = true;
private String filenameFilter;

  public FilesInFolderList(String name, boolean recurse) throws IOException {
	  this(name, recurse, null);
  }
  
  public FilesInFolderList(File directory, boolean recurse) throws IOException {
	  this(directory, recurse, null);
  }
  
  public FilesInFolderList(String name, boolean recurse, String filenameFilter) throws IOException {
    this(new File(name), recurse, filenameFilter);
    //recurseFolderFlag = false;
  }

  public FilesInFolderList(File directory, boolean recurse, String filenameFilter) throws IOException {
	  this.recurseFolderFlag = recurse;

	  if(filenameFilter != null) {
		  filenameFilter = filenameFilter.trim();
		  if(filenameFilter.length() == 0) {
			  filenameFilter = null;
		  }
	  }
	  this.filenameFilter = filenameFilter;
	  
    if (directory.isDirectory()) {
      this.fileFolder = new File(directory.getCanonicalPath());
    }
    else {
      throw new IOException(directory.toString() + " is not a directory");
    }
    getFiles(this.fileFolder);
  }

  private void getFiles(File folder){
    File[] files = folder.listFiles(new FileFilter() {
		
		@Override
		public boolean accept(File pathname) {
			if(filenameFilter == null) {
				return true;
			}
			//
			// Only filter files, not directories
			// and the like
			//
			if(!pathname.isFile()) {
				return true;
			}
			String name = pathname.getName();
			return name.matches(filenameFilter);
		}
	});
	
    for(int j = 0; j < files.length; j++) {
        this.fileFolderList.add(files[j]);
        if(files[j].isDirectory()){
            this.folderList.add(files[j]);
            if (recurseFolderFlag){
                getFiles(files[j]);
            }
        } else {
            this.fileArrayList.add(files[j]);
        }
    }
  }

  public long getFileListSize(){
      return this.fileArrayList.size();
  }

  public ArrayList<File> getfileArrayList() {
      return this.fileArrayList;
  }

  public static void main(String[] args) throws IOException {

      String testFolder = "/Volumes/HD2/appTests/f2e/data/pickup";
     
  }
  
  public static String getExtraPath(String rootPath, File file) throws IOException {
	   String rpDir = (new File(rootPath)).toString();
	   String flPath = file.getCanonicalPath();
	   String flName = file.getName();
	   
	   // System.err.println(rpDir + ":" + flPath + ":" + flName);
	   //
	   // Set the cursor start to the end of the path...
	   // compensate for missing directory headers "c:", etc...
	   //
	   int cursorStart = rpDir.length() + flPath.indexOf(rpDir);
	   int flPathLen = flPath.length();
	   int flNameLen = flName.length();
	   //
	   // Flip-flop the direction of the windows linefeeds
	   // to look like unix ones
	   //
	   return flPath.substring(cursorStart,  flPathLen - flNameLen - 1).replace('\\', '/');
  }

  
    public static String getExtraPathOld(String rootPath, File file) throws IOException {
       // gets the extra path that is in addition to the rootPath
       // if this includes a file name the name is removed as well with leading "/"
       //only works for a file class that is a file and not a folder or abstract path


       String fullPath = file.getCanonicalPath();
       String tempString = fullPath.replaceFirst("^" + rootPath, "");
       String fileNameRegEx = "/" + file.getName() + "$";
       //System.out.println("fileNameRegEx:" +  fileNameRegEx);
       String temp2String = tempString.replaceAll(fileNameRegEx, "");
       return temp2String;

    }
}
